package com.trex.modules.tbl.checkman.web;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.sys.utils.UserUtils;
import com.trex.modules.tbl.checkman.entity.TblZdJcr;
import com.trex.modules.tbl.checkman.service.TblZdJcrService;

/**
 * 检车人信息Controller
 * @author jy 
 * @version 2022-09-06
 */
@Controller
@RequestMapping(value = "/tbl/checkman/tblZdJcr")
public class TblZdJcrController extends BaseController {

	@Autowired
	private TblZdJcrService tblZdJcrService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public TblZdJcr get(String recId, boolean isNewRecord) {
		return tblZdJcrService.get(recId, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	//@RequiresPermissions("tbl:checkman:tblZdJcr:view")
	@RequestMapping(value = {"list", ""})
	public String list(@RequestParam(value = "userid", required = false) String userid,
					   @RequestParam(value = "postid", required = false) String postid,TblZdJcr tblZdJcr, Model model) {

		model.addAttribute("tblZdJcr", tblZdJcr);
		return "modules/tbl/checkman/tblZdJcrList";
	}
	
	/**
	 * 查询列表数据
	 */
	//@RequiresPermissions("tbl:checkman:tblZdJcr:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<TblZdJcr> listData(TblZdJcr tblZdJcr, HttpServletRequest request, HttpServletResponse response) {
		tblZdJcr.setPage(new Page<>(request, response));
		Page<TblZdJcr> page = tblZdJcrService.findPage(tblZdJcr);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	//@RequiresPermissions("tbl:checkman:tblZdJcr:view")
	@RequestMapping(value = "form")
	public String form(TblZdJcr tblZdJcr, Model model) {
		model.addAttribute("tblZdJcr", tblZdJcr);
		return "modules/tbl/checkman/tblZdJcrForm";
	}

	/**
	 * 保存数据
	 */
	//@RequiresPermissions("tbl:checkman:tblZdJcr:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated TblZdJcr tblZdJcr, HttpServletRequest request) {
		String I_IP_ADDR = request.getRemoteHost(); // 取IP
		String I_USERCODE = UserUtils.getUser().getUserCode(); // 取用户id
		String I_USERNAME = UserUtils.getUser().getUserName(); //用户名
		
		tblZdJcr.setIp(I_IP_ADDR);
		tblZdJcr.setUserCode(I_USERCODE);
		tblZdJcr.setUserName(I_USERNAME);
		tblZdJcr.setUpdTime(new Date());
		
		TblZdJcr tblZdJcr_new = new TblZdJcr();
		tblZdJcr_new.setAreaCode(tblZdJcr.getAreaCode());
		tblZdJcr_new.setPlaceCode(tblZdJcr.getPlaceCode());
		//tblZdJcr_new.setJcrName(tblZdJcr.getJcrName());
		tblZdJcr_new.setTeam(tblZdJcr.getTeam());
		
		if(tblZdJcr.getIsNewRecord()) {
			//新增 判断记录是否已存在
			if(tblZdJcrService.findCount(tblZdJcr_new) > 0) {
				return renderResult(Global.FALSE, text("检车人信息已存在，无法新增！"));
			};
		}else {
			//修改
			List<TblZdJcr> listU = tblZdJcrService.findList(tblZdJcr_new);

			for(int a=0;a<listU.size();a++) {
				TblZdJcr tblZdJcr_new2 = listU.get(a);
				if(tblZdJcr_new2.getRecId().equals(tblZdJcr.getRecId())) {
					continue;
				}else {
					return renderResult(Global.FALSE, text("检车人信息已存在，修改失败！"));
				}
			}
		}
		tblZdJcrService.save(tblZdJcr);
		return renderResult(Global.TRUE, text("保存检车人信息成功！"));
	}
	
	/**
	 * 删除数据
	 */
	//@RequiresPermissions("tbl:checkman:tblZdJcr:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(TblZdJcr tblZdJcr) {
		tblZdJcrService.delete(tblZdJcr);
		return renderResult(Global.TRUE, text("删除检车人信息成功！"));
	}
	
}